package com.example.thread.demo.alternate.print5;

public class SynchronizedABC {

    private int count = 0;

    private final Object lock1 = new Object();
    private final Object lock2 = new Object();
    private final Object lock3 = new Object();

    class MultiTurningRunner implements Runnable {
        private final Object prev;
        private final Object self;
        private final String content;

        public MultiTurningRunner(Object prev, Object self, String content) {
            this.prev = prev;
            this.self = self;
            this.content = content;
        }

        @Override
        public void run() {
            while (count <= 100) {
                synchronized (prev) {
                    synchronized (self) {
                        System.out.println(content);
                        count++;
                        self.notifyAll();
                    }
                    try {
                        if (count <= 100) {
                            prev.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public void multiTurning1() {

        Thread t1 = new Thread(() -> {
            while (count <= 100) {
                synchronized (lock2) {
                    synchronized (lock1) {
                        System.out.println("线程1：1");
                        count++;
                        // 唤醒正在等待lock1 的 t3
                        lock1.notifyAll();
                    }
                    try {
                        // 如果还需要继续执行，则让出lock2 让 t2 执行，自己进入等待状态
                        if (count <= 100) {
                            lock2.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread t2 = new Thread(() -> {
            while (count <= 100) {
                synchronized (lock3) {
                    synchronized (lock2) {
                        System.out.println("线程2：2");
                        count++;
                        lock2.notifyAll();
                    }
                    try {
                        if (count <= 100) {
                            lock3.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread t3 = new Thread(() -> {
            while (count <= 100) {
                synchronized (lock1) {
                    synchronized (lock3) {
                        System.out.println("线程3：3");
                        count++;
                        lock3.notifyAll();
                    }
                    try {
                        if (count <= 100) {
                            lock1.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();
    }

    public void multiTurning2() {
        Thread t1 = new Thread(new MultiTurningRunner(lock2, lock1, "线程1：1"));
        Thread t2 = new Thread(new MultiTurningRunner(lock3, lock2, "线程2：2"));
        Thread t3 = new Thread(new MultiTurningRunner(lock1, lock3, "线程3：3"));
        t1.start();
        t2.start();
        t3.start();
    }

    public static void main(String[] args) {
        SynchronizedABC solution = new SynchronizedABC();
//        solution.multiTurning1();
        solution.multiTurning2();

    }
}
